package i18n;

import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * Classe amb metòdes estàtics per la gestió de les opcions d'idioma.
 * 
 * @author Josep M Sobrepere
 *
 */
public class LanguageUtils
{
	private static ResourceBundle resourceBundle;
	private static String currentBaseName;
	private static Locale currentLocale;

	/**
	 * @param paramString
	 * @return retorna true si no hi ha hagut problemes, false si han saltat excepcions
	 */
	public static synchronized boolean setDefaultLanguage(String paramString)
	{
		try
		{
			currentBaseName = paramString;
			currentLocale=Locale.getDefault();
			resourceBundle = ResourceBundle.getBundle(paramString,currentLocale);

			return true;
		} catch (MissingResourceException localMissingResourceException) {
			localMissingResourceException.printStackTrace();
		}return false;
	}

	/**
	 * @param paramString
	 * @param paramLocale
	 * @return true si no hi ha hagut problemes, false si han saltat excepcions
	 */
	public static synchronized boolean setLanguage(String paramString, Locale paramLocale)
	{
		try
		{
			currentBaseName = paramString;
			currentLocale = paramLocale;
			resourceBundle = ResourceBundle.getBundle(paramString, paramLocale);
			return true;
		} catch (MissingResourceException localMissingResourceException) {
			localMissingResourceException.printStackTrace();
		}return false;
	}

	/**
	 * @param paramString
	 * @return retorna un missatge en l'idioma local
	 */
	public static String getMessage(String key)
	{
		//System.out.println("buscant: "+key);
		try
		{
			return resourceBundle.getString(key);
	    } 
		catch (MissingResourceException e) {
		}
		return new String();
	}

	/**
	 * @return currentBaseName
	 */
	public static String getCurrentBaseName()
	{
		return currentBaseName;
	}

	/**
	 * @return currentLocale
	 */
	public static Locale getCurrentLocale()
	{
		return currentLocale;
	}
}